package test;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by kevin on 2020/2/9.
 */
public class HttpRequestUtils {

        /**
         * GET提交
         *
         * @return
         */
        public static String doGet(String url,String authorization) {
            String strResult = "";
            // 1. 创建一个默认的client实例
            CloseableHttpClient client = HttpClients.createDefault();
            try {
                // 2. 创建一个httpget对象
                HttpGet httpGet = new HttpGet(url);
                httpGet.addHeader("x-wlk-Authorization", authorization); //添加请求头

                // 3. 执行GET请求并获取响应对象
                CloseableHttpResponse resp = client.execute(httpGet);
                try {
                    // 6. 打印响应长度和响应内容
                    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        // 4. 获取响应体
                        HttpEntity entity = resp.getEntity();
                        strResult = EntityUtils.toString(resp.getEntity());
                    }
                } finally {
                    //无论请求成功与否都要关闭resp
                    resp.close();
                }
            } catch (ClientProtocolException e) {
                // e.printStackTrace();
            } catch (ParseException e) {
                // e.printStackTrace();
            } catch (IOException e) {
                // e.printStackTrace();
            } finally {
                // 8. 最终要关闭连接，释放资源
                try {
                    client.close();
                } catch (Exception e) {
                    // e.printStackTrace();
                }
            }
            return strResult;
        }

        /**
         * 普通POST提交
         * @param url
         * @param map
         * @return
         */
        public static String doPost(String url, Map<String, Object> map) {
            String strResult = "";
            // 1. 获取默认的client实例
            CloseableHttpClient client = HttpClients.createDefault();
            // 2. 创建httppost实例
            HttpPost httpPost = new HttpPost(url);
            // 3. 创建参数队列（键值对列表）
            List<NameValuePair> paramPairs = new ArrayList<>();
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                Object val = map.get(key);
                paramPairs.add(new BasicNameValuePair(key, val.toString()));
            }
            UrlEncodedFormEntity entity;
            try {
                // 4. 将参数设置到entity对象中
                entity = new UrlEncodedFormEntity(paramPairs, "UTF-8");
                // 5. 将entity对象设置到httppost对象中
                httpPost.setEntity(entity);
                // 6. 发送请求并回去响应
                CloseableHttpResponse resp = client.execute(httpPost);
                try {
                    // 7. 获取响应entity
                    HttpEntity respEntity = resp.getEntity();
                    strResult = EntityUtils.toString(respEntity, "UTF-8");
                } finally {
                    // 9. 关闭响应对象
                    resp.close();
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 10. 关闭连接，释放资源
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return strResult;
        }

        /**
         * json参数方式POST提交
         * @param url
         * @param params
         * @return
         */
        public static String doPost(String url, JSONObject params){
            String strResult = "";
            // 1. 获取默认的client实例
            CloseableHttpClient client = HttpClients.createDefault();
            // 2. 创建httppost实例
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); //添加请求头
            try {
                httpPost.setEntity(new StringEntity(params.toJSONString(),"utf-8"));
                CloseableHttpResponse resp = client.execute(httpPost);
                try {
                    // 7. 获取响应entity
                    HttpEntity respEntity = resp.getEntity();
                    strResult = EntityUtils.toString(respEntity, "UTF-8");
                } finally {
                    resp.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return strResult;
        }

    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //设置相应请求时间
            conn.setConnectTimeout(30000);
            //设置读取超时时间
            conn.setReadTimeout(30000);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println(e);
            return "发送 POST 请求出现异常！";
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}
